# -*- coding: utf-8 -*-

 ###############################################
 ##                                             ##
 ##  Basiq Empresa                               ##
 ##                                              ##
 ##                                              ##
 ##                                              ##
 ##  by Críptidos Digitales                      ##
 ##  GPL (c)2008                                 ##
  ##                                             ##
    ###############################################

"""
    Sistema Basiq Empresa

    Control de Entidades y Procesos en una empresa pequeña.


"""

import sys
from PyQt4 import QtCore, QtGui
import utilities
import principal
import access
import propio

import man
import configuracion

import personas.modelo
import productos.manejador

# import locale

# locale.setlocale(locale.LC_ALL, es_MX)


class MyApp(QtGui.QApplication):

    BASICO = 0
    TALLER = 1

    content = u'Básico'

    def __init__(self,  *args):
        QtGui.QApplication.__init__(self, *args)

        self.__config = configuracion.Parser()
        self.__screenGeometry = QtGui.QDesktopWidget().screenGeometry()
        self.__reach = 128
        self.__controller = {}

    @property
    def config(self):
        """property 'config', read only, class"""
        return self.__config

    def controller(self, type):
        return self.__controller[type]

    @property
    def distribution(self):
        """property 'distribution', read only, class"""
        return self.BASICO

    @property
    def fontZoom(self):
        if self.__screenGeometry.height() == 1024:
            return 1.6
        elif self.__screenGeometry.height() == 800:
            return 1.25
        elif self.__screenGeometry.height() <= 768:
            return 1.20
        else:
            return 1.0

    @property
    def form(self):
        return self.__form

    @property
    def info(self):
        """property 'info', read only, dict"""
        return {'version':u"0.2.0", 'title':u"Basiq Empresa", 'author':u"Jorge Hojyo", 'company':u"Críptidos Digitales", 'license':u"GPL (c) 2008", 'alias':u"basiqE"}

    def initDB(self):
        self.__status = man.inicializa()
        personas.modelo.inicializa()
        productos.manejador.inicializa()

    @property
    def reach(self):
        return self.__reach

    @property
    def screenGeometry(self):
        return self.__screenGeometry

    def setController(self, name, controller):
        self.__controller[name] = controller

    def setForm(self, form):
        self.__form = form

    def setReach(self, valor):
        self.__reach = valor


if __name__ == "__main__":
    app = MyApp(sys.argv)
    app.setQuitOnLastWindowClosed(True)

    formaAcceso = access.Form(app=app)
    result = formaAcceso.exec_()

    if result == QtGui.QDialog.Accepted:
        formaAcceso.destroy()

        app.initDB()

        if not man.runCount():
            formaDatos = propio.Forma(app=app)
            formaDatos.agrega()
            result = formaDatos.exec_()

        if result == QtGui.QDialog.Accepted:

            man.incrementRunCount()

            app.holder = man.rolI(tipo=u'propio')

            app.splash = QtGui.QSplashScreen(QtGui.QPixmap("./imagenes/logo.png"), QtCore.Qt.WindowStaysOnTopHint)
            app.splash.showMessage(u"Cargando %s %s ..." % (app.info['title'], app.info['version']), QtCore.Qt.AlignBottom, QtGui.QColor("#4040FF"))
            app.splash.show()
            app.processEvents()

            app.setReach(result)

            app.setForm(principal.Form(app=app))

            app.splash.finish(None)

            app.form.show()


            if man.atributoI(grupo=u'sistema', nombre=u'mostrarTips').valor == u"1":
                app.form.ui.tips.raise_()

            sys.exit(app.exec_())

    else:
        formaAcceso.destroy()

"""
    if man.testPassword("firstRun"):
        formaDatos = propio.Forma(app=app)
        result = formaDatos.exec_()
        if result == QtGui.QDialog.Accepted:
            formaContrasena = access.FormContrasena(app=app, modo="firstRun")
            result = formaContrasena.exec_()

            formaAcceso.setData(formaContrasena.ui.edContrasena.text())

    if result == QtGui.QDialog.Accepted:
        app.beep()
        result = formaAcceso.exec_()
        formaAcceso.destroy()

        app.splash = QtGui.QSplashScreen(QtGui.QPixmap(), QtCore.Qt.WindowStaysOnTopHint)
        # app.splash.setStyleSheet("background-color:#B0B070;")
        app.splash.pixmap().fill(QtGui.QColor("#B0B070"))
        app.splash.setPixmap(QtGui.QPixmap("./imagenes/logo.png"))
        # app.splash.pixmap().fill("red")
        # app.splash.repaint()
        app.splash.showMessage(u"Cargando %s %s" % (app.info['title'], app.info['version']), QtCore.Qt.AlignBottom, QtGui.QColor("#4040FF"))
        app.splash.show()
        app.processEvents()

        if result:
            app.setReach(result)

            # content = app.BASICO

            app.form = principal.form(owner=app, app=app)

            app.splash.finish(None)

            app.form.show()

            if man.dameAtributo(propietario=u'tips', nombre=u'mostrar').valor=="1":
                app.form.ui.tips.raise_()
            app.beep()
            result = app.exec_()
            sys.exit(result)
        else:
            app.splash.finish(None)

    else:
        formaAcceso.destroy()

"""
